package sketch.specs;

import java.util.List;
import java.util.Map;

import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.Comment;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.MethodDeclaration;

import sketch.ast.ASTUtils;
import sketch.specs.annotation.TestSketch;
import sketch.util.Checker;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;


public class BlockMappingVisitorTest extends TestCase {
	
	public static Test suite() {
        return new TestSuite(BlockMappingVisitorTest.class);
    }
	
	public void testBlockMapping() {
//		String filePath = "./tests/sketch/specs/samplecode.txt";
		String filePath = "./testfiles/sketch/specs/SampleCode.java";
		CompilationUnit unit = ASTUtils.astRoot(filePath);
		FileContents.addFile(filePath);
		List<Comment> cs = unit.getCommentList();
		//unit.getLineNumber(position)
		//cs = ASTUtils.blockComments(cs);
		BlockMappingVisitor visitor = new BlockMappingVisitor(cs);
		visitor.visit(unit);
		Map<Comment, Block> results = visitor.getCommentMappings();
		for(Comment comment : results.keySet()) {
			Block block = results.get(comment);
			System.out.println(block);
			//let's fetch out the comment
			String commentContent = FileContents.fetchString(filePath,
					unit.getLineNumber(comment.getStartPosition()), unit.getColumnNumber(comment.getStartPosition()),
					unit.getLineNumber(comment.getStartPosition() + comment.getLength()),
					unit.getColumnNumber(comment.getStartPosition() + comment.getLength()));
			System.out.println("Comment: " + commentContent);
			System.out.println("\n\n\n");
		}
		assertTrue(results.size() == 10);
		assertTrue(visitor.getBlockMappings().size() == 10);
	}
	
	public void testFetchTestSketch() {
		String filePath = "./testfiles/sketch/specs/SampleCode.java";
		CompilationUnit unit = ASTUtils.astRoot(filePath);
		List<MethodDeclaration> methods = ASTUtils.getMethodsWithAnnotation(unit, "TestSketch");
		assertTrue(methods.size() == 7);
	}
	
	public void testVisitMethodDecl() {
		String filePath = "./testfiles/sketch/specs/SampleCode.java";
		CompilationUnit unit = ASTUtils.astRoot(filePath);
		List<MethodDeclaration> methods = ASTUtils.getMethodsWithAnnotation(unit, "TestSketch");
		//get the comment list
		List<Comment> cs = unit.getCommentList();
		//unit.getLineNumber(position)
		//cs = ASTUtils.blockComments(cs);
		for(MethodDeclaration method : methods) {
			System.out.println("  ---- processing method: " + method.getName() + " ---- ");
			BlockMappingVisitor visitor = new BlockMappingVisitor(cs);
			visitor.visit(method);
			Map<Block, Comment> blockCommentMappings = visitor.getBlockMappings();
			for(Block block : blockCommentMappings.keySet()) {
				Comment comment = blockCommentMappings.get(block);
				System.out.println(block);
				String commentContent = FileContents.fetchString(filePath,
						unit.getLineNumber(comment.getStartPosition()), unit.getColumnNumber(comment.getStartPosition()),
						unit.getLineNumber(comment.getStartPosition() + comment.getLength()),
						unit.getColumnNumber(comment.getStartPosition() + comment.getLength()));
				System.out.println("Comment: " + commentContent);
				//show its parent
				ASTNode parent = block.getParent();
				System.out.println("parent: ");
				Checker.checkTrue(parent instanceof Block, "The parent is not a Block type, it is: " + parent.getClass());
				System.out.println(parent);
				System.out.println("\n\n");
			}
		}
	}
	
	public void testTestSketch() {
		assertTrue(TestSketch.class.getSimpleName().equals("TestSketch"));
	}
}
